//订单查询
(function(vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            storeOrderCartInfo: {
                orders: [],
                total: 0,
                records: 1,
                orderDetail: false,
                moreCondition: false,
                states: [],
                cartTypes: [],//3306商品 3307 家政服务
                conditions: {
                    cartId: '',
                    orderId: '',
                    state: '',
                    personName: '',
                    cartType: '',
                    prodName: '',
                    shopId: ''
                },
                address: {
                    username: '',
                    tel: '',
                    address: ''
                },
                curOrderCart: {}
            }
        },
        _initMethod: function() {
            $that._listOrders(DEFAULT_PAGE, DEFAULT_ROWS);
            vc.getDict('store_order', "state", function(_data) {
                $that.storeOrderCartInfo.states = _data;
            });
            vc.getDict('store_order_cart', "cart_type", function(_data) {
				console.log("类型：",_data)
                $that.storeOrderCartInfo.cartTypes = _data;
            });
        },
        _initEvent: function() {
            vc.on('storeOrderCart', 'goBack', function(_param) {
                $that.storeOrderCartInfo.orderDetail = false;
            });
            vc.on('storeOrderCart', 'list', function() {
                $that._listOrders(DEFAULT_PAGE, DEFAULT_ROWS);
            });
            vc.on('pagination', 'page_event', function(_currentPage) {
                $that._listOrders(_currentPage, DEFAULT_ROWS);
            });
        },
        methods: {
            _listOrders: function(_page, _rows) {
                $that.storeOrderCartInfo.conditions.page = _page;
                $that.storeOrderCartInfo.conditions.row = _rows;
                $that.storeOrderCartInfo.conditions.shopId = vc.getCurrentCommunity().shopId;
                let param = {
                    params: $that.storeOrderCartInfo.conditions
                };
                param.params.orderId = param.params.orderId.trim();
                param.params.prodName = param.params.prodName.trim();
                param.params.personName = param.params.personName.trim();
                //发送get请求
                vc.http.apiGet('/storeOrder.queryStoreOrder',
                    param,
                    function(json, res) {
                        let _json = JSON.parse(json);
                        $that.storeOrderCartInfo.total = _json.total;
                        $that.storeOrderCartInfo.records = _json.records;
                        $that.storeOrderCartInfo.orders = _json.data;
                        let _orders = $that.storeOrderCartInfo.orders;
                        _orders.forEach(item => {
                            if (item.hasOwnProperty('productSpecDetails')) {
                                let _productSpecDetails = item.productSpecDetails;
                                let _specValue = '';
                                _productSpecDetails.forEach(detail => {
                                    _specValue += (detail.detailValue + "/");
                                });
                                item.specValue = _specValue;
                            }
                        });
                        vc.emit('pagination', 'init', {
                            total: $that.storeOrderCartInfo.records,
                            dataCount: $that.storeOrderCartInfo.total,
                            currentPage: _page
                        });
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            //查询
            _queryOrdersMethod: function() {
                $that._listOrders(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            //重置
            _resetOrdersMethod: function() {
                $that.storeOrderCartInfo.conditions.orderId = "";
                $that.storeOrderCartInfo.conditions.cartType = "";
                $that.storeOrderCartInfo.conditions.state = "";
                $that.storeOrderCartInfo.conditions.prodName = "";
                $that.storeOrderCartInfo.conditions.personName = "";
                $that._listOrders(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _openOrderDetailModel: function(_order) {
                vc.jumpToPage('/#/pages/goods/storeOrderCartDetail?orderId=' + _order.orderId);
            },
            _openSendOrderCartModel: function(_order) { //发货方法
               vc.emit('sendStoreOrderCart', 'openSendStoreOrderCartModel',_order);
            },
            _toStockUpOrder:function(_order){
                vc.jumpToPage('/#/pages/order/stockUpStoreOrder?orderId=' + _order.orderId);

            },
            clearAddress: function() {
                $that.storeOrderCartInfo.address = {
                    username: '',
                    tel: '',
                    address: ''
                }
            },
            _moreCondition: function() {
                if ($that.storeOrderCartInfo.moreCondition) {
                    $that.storeOrderCartInfo.moreCondition = false;
                } else {
                    $that.storeOrderCartInfo.moreCondition = true;
                }
            },
            _openOrderPrint: function(_order) {
                //window.open('/smallPrint.html#/pages/goods/printSmallOrder?orderId=' + _order.orderId);
                let _paramIn = {
                    shopId: vc.getCurrentCommunity().shopId,
                    orderId: _order.orderId
                }
                vc.http.apiPost(
                    '/printer.orderPrinter',
                    JSON.stringify(_paramIn),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _exportOrder: function () {
                $that.storeOrderCartInfo.conditions.pagePath = 'storeOrderCart';
                let param = {
                    params: $that.storeOrderCartInfo.conditions
                };

                //发送get请求
                vc.http.apiGet('/export.exportData', param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        vc.toast(_json.msg);
                        if (_json.code == 0) {
                            vc.jumpToPage('/#/pages/property/downloadTempFile?tab=下载中心&fileType=storeOrderCart')
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    });
            },

        }
    });
})(window.vc);
